home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Developer / StopWatch / Source / BrowserViewMgr.m < prev    next >
Encoding:
Text File  |  1994-02-06  |  5.6 KB  |  282 lines

  1. /*
  2.  * For legal stuff see the file COPYRIGHT
  3.  */
  4. #import "BrowserViewMgr.h"
  5. #import "ClientInspector.h"
  6. #import "Controller.h"
  7. #import "ColCell.h"
  8. #import "SessionEditor.h"    /* later, this should be the abstract superclass... */
  9.  
  10. @implementation BrowserViewMgr
  11.  
  12. - awakeFromNib
  13. {
  14.   [browser setCellClass:[ColCell class]];
  15.   [browser setTarget:self];
  16.   [browser setAction:@selector(browserClick:)];
  17.   [browser setDoubleAction:@selector(browserDoubleClick:)];
  18.   selectedCells = [[List alloc] init];
  19.   return self;
  20. }
  21.  
  22. - mgrShow:(ClientInspector *)inspector
  23. {
  24.   [self displayTitle];        /* we do this in our own text field */
  25.   [browser loadColumnZero];
  26.   [self displaySummary];
  27.   return self;
  28. }
  29.  
  30. /*
  31.  * On a double-click in the main client browser, add a new
  32.  * item for the selected client.
  33.  */
  34. - (BOOL)mgrDoubleClick:(ClientInspector *)inspector
  35. {
  36.   return [self mgrAdd:inspector];
  37. }
  38.  
  39. /*
  40.  * Assumes the cells in the browsers respond to cellData method.
  41.  */
  42. - (void)selectCellWithItem:item
  43. {
  44.   Matrix *matrix = [browser matrixInColumn:0];
  45.   List *list = [matrix cellList];
  46.   ColCell *cell;
  47.   int i, count = [list count];
  48.  
  49.   for ( i = 0; i < count; i++ ) {
  50.     cell = [list objectAt:i];
  51.     if ( [cell cellData] == item ) {
  52.       //[matrix highlightCellAt:i :0 lit:YES];
  53.       [matrix selectCell:cell];
  54.       [matrix scrollCellToVisible:i :0];
  55.     }
  56.   }
  57. }
  58.  
  59. /*
  60.  * Allow the user to fill in a new item manually.
  61.  */
  62. - (BOOL)mgrAdd:(ClientInspector *)inspector
  63. {
  64.   ClientInfo *info = [inspector selectedClient];
  65.   id item = [[self itemEditor] editItem:nil]; /* new method: editItem */
  66.  
  67.   if ( item == nil )
  68.     return NO;
  69.  
  70.   [info perform:[self addMethod] with:item];
  71.   [self mgrShow:inspector];
  72.   [self selectCellWithItem:item];
  73.   return YES;
  74. }
  75.  
  76. /*
  77.  * Delete the selected expense, saving it on the undelete list
  78.  */
  79. - (BOOL)mgrDelete:(ClientInspector *)inspector
  80. {
  81.   ClientInfo *info = [inspector selectedClient];
  82.   int i, count;
  83.   id item;
  84.   Matrix *matrix;
  85.  
  86.   [selectedCells empty];
  87.   [browser getSelectedCells:selectedCells];
  88.  
  89.   if ( (count = [selectedCells count]) == 0 )
  90.     return NO;
  91.  
  92.   for ( i = 0; i < count; i++ ) {
  93.     item = [[selectedCells objectAt:i] cellData];
  94.     [info perform:[self deleteMethod] with:item];
  95.     [self saveDeletedItem:item];
  96.   }
  97.  
  98.   [self mgrShow:inspector];
  99.  
  100.   /* Select the first cell, for lack of anything better... */
  101.   matrix = [browser matrixInColumn:0];
  102.   [matrix scrollCellToVisible:0 :0];
  103.   [matrix selectCellAt:0 :0];
  104.  
  105.   return YES;
  106. }
  107.  
  108. - (BOOL)mgrUndelete:(ClientInspector *)inspector
  109. {
  110.   id item = [self getDeletedItem];
  111.   ClientInfo *info = [inspector selectedClient];
  112.  
  113.   if ( item ) {
  114.     [info perform:[self addMethod] with:item];
  115.     // Select the added item!
  116.     [self mgrShow:inspector];
  117.     [self selectCellWithItem:item];
  118.     return YES;
  119.   }
  120.  
  121.   return NO;            /* indicate that no save is required */
  122. }
  123.  
  124. /*
  125.  * Modify the selected expense.
  126.  */
  127. - (BOOL)mgrModify:(ClientInspector *)inspector
  128. {
  129.   ClientInfo *info = [inspector selectedClient];
  130.   id item = [self selectedItem];
  131.  
  132.   if ( item == nil || [[self itemEditor] editItem:item] == nil )
  133.     return NO;
  134.  
  135.   [info perform:[self sortMethod]];
  136.   [info computeTotalMins];
  137.   [self mgrShow:inspector];
  138.   [self selectCellWithItem:item];
  139.   return YES;
  140. }
  141.  
  142. - (BOOL)canAdd
  143. {
  144.   return YES;
  145. }
  146.  
  147. /*
  148.  * We can use the modify only if there is exactly one
  149.  * item selected.
  150.  */
  151. - (BOOL)canModify
  152. {
  153.   [selectedCells empty];
  154.   [browser getSelectedCells:selectedCells];
  155.  
  156.   return ([selectedCells count] == 1 ? YES : NO);
  157. }
  158.  
  159. /*
  160.  * We can use the delete button if there are any items selected.
  161.  */
  162. - (BOOL)canDelete
  163. {
  164.   return [[browser matrixInColumn:0] selectedCell] ? YES : NO;
  165. }
  166.  
  167. - (SEL)addMethod
  168. {
  169.   [self subclassResponsibility:_cmd];
  170.   return (SEL)0;
  171. }
  172.  
  173. - (SEL)deleteMethod
  174. {
  175.   [self subclassResponsibility:_cmd];
  176.   return (SEL)0;
  177. }
  178.  
  179. - (SEL)sortMethod
  180. {
  181.   [self subclassResponsibility:_cmd];
  182.   return (SEL)0;
  183. }
  184.  
  185. - itemEditor
  186. {
  187.   [self subclassResponsibility:_cmd];
  188.   return self;
  189. }
  190.  
  191. /*
  192.  * This must be implemented per subclass, so the right list is accessed
  193.  */
  194. - info:(ClientInfo *)info itemAt:(int)position
  195. {
  196.   [self subclassResponsibility:_cmd];
  197.   return self;
  198. }
  199.  
  200. /*
  201.  * Must be implemented per subclass
  202.  */
  203. - (int)itemCount:(ClientInfo *)info
  204. {
  205.   [self subclassResponsibility:_cmd];
  206.   return 0;
  207. }
  208.  
  209. - (void)displayTitle
  210. {
  211.   [self subclassResponsibility:_cmd];
  212. }
  213.  
  214.  
  215. @implementation BrowserViewMgr(PRIVATE)
  216.  
  217. - (int)selectedRow
  218. {
  219.   return [[browser matrixInColumn:0] selectedRow];
  220. }
  221.  
  222. - selectedItem
  223. {
  224.   ClientInfo *info = [[ClientInspector sharedInstance] selectedClient];
  225.  
  226.   return [self info:info itemAt:[self selectedRow]];
  227. }
  228.  
  229. - (void)displaySummary
  230. {
  231.   [self subclassResponsibility:_cmd];
  232. }
  233.  
  234. - selectAll:sender
  235. {
  236.   id result = [browser selectAll:sender];
  237.   [ClientInspector updateButtons];
  238.   return result;
  239. }
  240.  
  241. - browserClick:sender
  242. {
  243.   [ClientInspector updateButtons];
  244.   return self;
  245. }
  246.  
  247. - browserDoubleClick:sender
  248. {
  249.   /*
  250.    * Since the cell is still activated, make this happen next event pass. This
  251.    * seems to be a bug in NXBrowser, since this only is necessary if multiple
  252.    * select is enabled...
  253.    */
  254.   [[ClientInspector sharedInstance] perform:@selector(performModify)
  255.      with:nil afterDelay:1 cancelPrevious:YES];
  256.  
  257.   return nil;
  258. }
  259.  
  260. - (int)browser:sender fillMatrix:matrix inColumn:(int)column
  261. {
  262.   ClientInfo *info = [[ClientInspector sharedInstance] selectedClient];
  263.   int i, count = [self itemCount:info];
  264.   ColCell *cell;
  265.   id item;
  266.  
  267.   [matrix renewRows:0 cols:0];
  268.  
  269.   for ( i = 0; i < count; i++ ) {
  270.     [matrix addRow];
  271.     item = [self info:info itemAt:i];
  272.     cell = [matrix cellAt:i :0];
  273.     [cell setCellData:item];        /* our custom cell draws multi-column */
  274.     [cell setLoaded:YES];
  275.     [cell setLeaf:YES];
  276.   }
  277.  
  278.   return count ;
  279. }
  280.  
  281. @end
  282.